package prv.pursuivant.unimportant.scraps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import prv.pursuivant.config.CategorySerializer;
import prv.pursuivant.config.impl.XStreamCategorySerializerModule;
import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievTypesTree;
import prv.pursuivant.core.AchievTypesTreeImpl;
import prv.pursuivant.core.impl.CategoryAchievType;
import prv.pursuivant.core.impl.NormalAchievType;
import prv.pursuivant.core.impl.NpcAchievType;

import com.google.inject.Guice;

public class Translator {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		OldAchiev main = OldAchiev.importData("168");
		OldAchiev emblems = OldAchiev.importData("emblems");
		OldAchiev tBC = OldAchiev.importData("14805");
		OldAchiev lKDungeon = OldAchiev.importData("14806");
		OldAchiev vanilla = OldAchiev.importData("14808");
		OldAchiev lKHero = OldAchiev.importData("14921");
		OldAchiev lKRaid10 = OldAchiev.importData("14922");
		OldAchiev lKRaid25 = OldAchiev.importData("14923");
		OldAchiev lKUld10 = OldAchiev.importData("14961");
		OldAchiev lKUld25 = OldAchiev.importData("14962");
		List<AchievType> all = new ArrayList<AchievType>();
		List<String> criteria = new ArrayList<String>();
		String id = main.id.toString();
		criteria.add(createNewAchiev(emblems, all, id, "").getId());
		criteria.add(createNewAchiev(vanilla, all, id, "").getId());
		criteria.add(createNewAchiev(tBC, all, id, "").getId());
		criteria.add(createNewAchiev(lKDungeon, all, id, "").getId());
		criteria.add(createNewAchiev(lKHero, all, id, "").getId());
		criteria.add(createNewAchiev(lKRaid10, all, id, "").getId());
		criteria.add(createNewAchiev(lKRaid25, all, id, "").getId());
		criteria.add(createNewAchiev(lKUld10, all, id, "").getId());
		criteria.add(createNewAchiev(lKUld25, all, id, "").getId());
		for (OldAchiev crit : main.criteria) {
			AchievType c = createNewAchiev(crit, all, id, "");
			criteria.add(c.getId());
		}
		CategoryAchievType cat = new CategoryAchievType(main.name, id,
				criteria);
		AchievTypesTree tree =new AchievTypesTreeImpl(all, cat); 

		CategorySerializer cs = Guice.createInjector(
				new XStreamCategorySerializerModule()).getInstance(
				CategorySerializer.class);
		cs.writeTree(tree, System.out);
	}

	private static AchievType createNewAchiev(OldAchiev achiev,
			List<AchievType> allTheAchievements, String parentId, String stack) {
		stack += "/" + achiev.name + "(" + achiev.id + ")";
		System.out.println("Processing " + stack);
		List<String> critNames = new ArrayList<String>();
		if (null != achiev.criteria)
			for (OldAchiev oldCrit : achiev.criteria) {
				AchievType crit = createNewAchiev(oldCrit, allTheAchievements,
						achiev.id == null ? achiev.name : achiev.id, stack);
				critNames.add(crit.getId());
			}
		AchievType result = achiev.id == null ? new NpcAchievType(achiev.name,
				parentId) : new NormalAchievType(achiev.name, achiev.id,
				critNames);
		if (allTheAchievements.contains(result)) {
			System.out.println(result.getId() + " already inserted, we're in "
					+ parentId);
		} else {
			System.out.println("Adding " + result.getId());
			allTheAchievements.add(result);
		}
		return result;
	}

}
